<template>
  <div >
    <el-card style="width: 100%;height: 500px">
      <div slot="header" class="clearfix header" style="padding: 0">
          <span>销售额类别占比</span>
          <div class="groupButton">
            <el-radio-group  size="small" style="margin-bottom: 5px;margin-left: 430px" v-model="radio3">
              <el-radio-button label="全部渠道"></el-radio-button>
              <el-radio-button label="线上" ></el-radio-button>
              <el-radio-button label="门店"></el-radio-button>
            </el-radio-group>
          </div>
      </div>
      <div class="main">
        <div class="charts" ref="charts">
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import* as echarts from 'echarts'
import {mapState} from "vuex";

export default {
  name: "Category",
  data(){
    return{
      radio3:'全部渠道',
      data1:[],
      data2:[],
      SaleTotal:[],
      charts:null
    }
  },
  mounted() {
    this.charts = echarts.init(this.$refs.charts)
    let option = {
      title:{
          left:'center',
          top:'center'
      },
      tooltip: {
        trigger: 'item'
      },
      legend: {
        top: '5%',
        left: 'right',
        orient:'vertical'
      },
      series: [
        {
          name: 'Access From',
          type: 'pie',
          radius: ['40%', '70%'],
          avoidLabelOverlap: false,
          itemStyle: {
            borderRadius: 10,
            borderColor: '#fff',
            borderWidth: 2
          },
          label: {
            show: true,
            position: 'outside',
          },
          emphasis: {
            label: {
              show: true,
              fontSize: '25',
              fontWeight: 'bold'
            }
          },
          labelLine: {
            show: true
          },
        }
      ]
    };
    this.charts.setOption(option)
    this.charts.on('mouseover',(params) =>{
      const {name , value} = params.data
      this.charts.setOption({
        title:{
          text:name,
          subtext:value
        }
      })
    });
  },
  computed:{
    ...mapState({
      saleRank:state => state.home.SaleData.saleRank
    }),

  },
  watch:{
    saleRank(){
       let name1 = this.saleRank.online.name
       let name2 = this.saleRank.shop.name
       let value = []
       let shop = this.saleRank.shop.value
       let online = this.saleRank.online.value
       for(let i=0;i < online.length ; i++){
          value.push(shop[i] + online[i])
       }
      for(let i=0;i < shop.length ; i++){
        this.SaleTotal.push({
          name:name1[i],
          value:value[i]
        })
        this.data1.push({
          name:name1[i],
          value:online[i]
        })
        this.data2.push({
          name:name2[i],
          value:shop[i]
        })
      }
    },
    SaleTotal(){
      let list = []
      this.charts.setOption({
        series: {
          data:this.SaleTotal
        }
      })
    },
    radio3(){
      if(this.radio3 == '全部渠道'){
        this.charts.setOption({
          series: {
            data:this.SaleTotal
          }
        })
      }else if(this.radio3 == '线上'){
        this.charts.setOption({
          series: {
            data:this.data1
          }
        })
      }else {
        this.charts.setOption({
          series: {
            data:this.data2
          }
        })
      }
    }
  }
}
</script>

<style scoped>
.header {
  display: flex;
  border-bottom: 1px solid #eee;
  padding: 0 0;
  justify-content: space-between;
  align-items: center;
}
.charts{
  width: 100%;
  height: 300px;
}

</style>
